﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多线程
{
    public class ImageManager
    {
        static int a = 0;
        /// <summary>
        /// 图像切割
        /// </summary>
        /// <param name="url">图像文件名称</param>
        /// <param name="width">切割后图像宽度</param>
        /// <param name="height">切割后图像高度</param>
        /// <param name="savePath">切割后图像文件保存路径</param>
        /// <param name="fileExt">切割后图像文件扩展名</param>
        public static void Cut(string url, int width, int height, string savePath, string fileExt)
        {
            Bitmap bitmap = new Bitmap(url);
            Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
            Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
            for (decimal i = 0; i < MaxRow; i++)
            {
                for (decimal j = 0; j < MaxColumn; j++)
                {
                    string filename = i.ToString() + "," + j.ToString() + "." + fileExt;
                    Bitmap bmp = new Bitmap(width, height);
                    for (int offsetX = 0; offsetX < width; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < height; offsetY++)
                        {
                            if (((j * width + offsetX) < bitmap.Width) && ((i * height + offsetY) < bitmap.Height))
                            {
                                bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
                            }
                        }
                    }
                    //Graphics g = Graphics.FromImage(bmp);
                    //g.DrawString("名称", new Font("黑体", 20), new SolidBrush(Color.FromArgb(70, Color.WhiteSmoke)), 60, height / 2);//加水印
                    ImageFormat format = ImageFormat.Png;
                    switch (fileExt.ToLower())
                    {
                        case "png":
                            format = ImageFormat.Png;
                            break;
                        case "bmp":
                            format = ImageFormat.Bmp;
                            break;
                        case "gif":
                            format = ImageFormat.Gif;
                            break;
                    }
                    bmp.Save(savePath + "//" + filename, format);
                }
            }
        }

        public static Bitmap[] Bimpa(string url, int width, int height, string savePath, string fileExt)
        {
            a = 0;
            Bitmap bitmap = new Bitmap(url);
            Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
            Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
            var row = MaxRow * MaxColumn;
            Bitmap[] newbit = new Bitmap[decimal.ToInt32(row)];
            Task.Run(() =>
            {
                Bimp(decimal.ToInt32(row), width, height, newbit);
            });
            Task.Run(() =>
            {
                Bimp0(decimal.ToInt32(row), width, height, newbit);
            }).Wait();

            return newbit;
        }
        public static Bitmap[] Bimp(int row, int width, int height , Bitmap[] newbit)
        {
            
            
            
            for (decimal i = 0; i < (row/2); i++)
            {
                    Bitmap bmp = new Bitmap(width, height);
                    for (int offsetX = 0; offsetX < width; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < height; offsetY++)
                        {
                                bmp.GetPixel(offsetX, offsetY);
                        }
                    }
                    newbit[a++] = new Bitmap(bmp.Width, bmp.Height);
                //}
            }
            return newbit;
        }
        public static Bitmap[] Bimp0(int row, int width, int height, Bitmap[] newbit)
        {
            
            for (decimal i = row; i > (row/2); i--)
            {
                Bitmap bmp = new Bitmap(width, height);
                for (int offsetX = 0; offsetX < width; offsetX++)
                {
                    for (int offsetY = 0; offsetY < height; offsetY++)
                    {
                        bmp.GetPixel(offsetX, offsetY);
                    }
                
                }
                newbit[a++] = new Bitmap(bmp.Width, bmp.Height);
                //}
            }
            return newbit;
        }

        public static Bitmap[] Bimp1(string url, int width, int height, string savePath, string fileExt)
        {
            int a = 0;
            Bitmap bitmap = new Bitmap(url);

            Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
            Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
            Bitmap[] newbit = new Bitmap[decimal.ToInt32(MaxRow * MaxColumn)];
            for (decimal i = 0; i < MaxRow; i++)
            {
                for (decimal j = 0; j < MaxColumn; j++)
                {
                    string filename = i.ToString() + "," + j.ToString() + "." + fileExt;
                    Bitmap bmp = new Bitmap(width, height);
                    for (int offsetX = 0; offsetX < width; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < height; offsetY++)
                        {
                            if (((j * width + offsetX) < bitmap.Width))
                            {
                                //bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
                                bmp.GetPixel(offsetX, offsetY);
                            }
                        }
                    }
                    newbit[a++] = new Bitmap(bmp.Width, bmp.Height);
                }
            }
            return newbit;
        }

        public static Bitmap[] Bimp2(string url, int width, int height, string savePath, string fileExt)
        {
            int a = 0;
            Bitmap bitmap = new Bitmap(url);

            Decimal MaxRow = Math.Ceiling((Decimal)bitmap.Height / height);
            Decimal MaxColumn = Math.Ceiling((decimal)bitmap.Width / width);
            Bitmap[] newbit = new Bitmap[decimal.ToInt32(MaxRow * MaxColumn/2)];
            for (decimal i = 0; i < MaxRow; i++)
            {
                for (decimal j = 0; j < MaxColumn; j++)
                {
                    string filename = i.ToString() + "," + j.ToString() + "." + fileExt;
                    Bitmap bmp = new Bitmap(width, height);
                    for (int offsetX = 0; offsetX < width; offsetX++)
                    {
                        for (int offsetY = 0; offsetY < height; offsetY++)
                        {
                            if (((j * width + offsetX) < bitmap.Width))
                            {
                                //bmp.SetPixel(offsetX, offsetY, bitmap.GetPixel((int)(j * width + offsetX), (int)(i * height + offsetY)));
                                bmp.GetPixel(offsetX, offsetY);
                            }
                        }
                    }
                    newbit[a++] = new Bitmap(bmp.Width, bmp.Height);
                }
            }
            return newbit;
        }
    }
}
